package com.yiqi.zhiyuan.feature.login.view;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.TextView;

import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.widget.PowerfulEditText;
import com.suozhang.framework.widget.dialog.NormalDialog;
import com.suozhang.framework.widget.dialog.OnBtnClickL;
import com.yiqi.zhiyuan.R;
import com.yiqi.zhiyuan.entity.enums.VCodeType;
import com.yiqi.zhiyuan.feature.login.contract.ResetContract;
import com.yiqi.zhiyuan.feature.login.dependencies.reset.DaggerResetComponent;
import com.yiqi.zhiyuan.feature.login.dependencies.reset.ResetPresenterModule;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.rong.imkit.RongIM;

/**
 * @ProjectName: ZhiYuan
 * @Package: com.yiqi.zhiyuan.feature.mine.view
 * @ClassName: UpdateLoginPwdActivity
 * @Description: 修改登录密码
 * @Author:
 * @CreateDate: 2019/3/12 17:21
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/3/12 17:21
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class EditPhoneActivity extends BaseActivity implements TextWatcher, ResetContract.View {


    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.edt_forget_phone)
    PowerfulEditText mEdtForgetPhone;
    @BindView(R.id.btn_forget_post_code)
    TextView mBtnForgetPostCode;
    @BindView(R.id.edt_forget_code)
    PowerfulEditText mEdtForgetCode;
    @BindView(R.id.edt_forget_pwd)
    PowerfulEditText mEdtForgetPwd;
    @BindView(R.id.btn_forget_password)
    TextView mBtnReset;

    @Inject
    ResetContract.Presenter mPresenter;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_update_login_pwd;
    }

    @Override
    protected void initInjector() {
        DaggerResetComponent.builder().resetPresenterModule(new ResetPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        initToolBar(mToolbar, getString(R.string.title_edit_phone), true, true);
    }

    @Override
    protected void initEvent() {
        mEdtForgetPhone.addTextChangedListener(this);
        mEdtForgetCode.addTextChangedListener(this);
    }

    @OnClick(R.id.btn_forget_post_code)
    public void onViewGetCodeClicked() {
        mPresenter.sendCode(mEdtForgetPhone.getText().toString().trim(), VCodeType.PHONE.type());
    }

    @OnClick(R.id.btn_forget_password)
    public void onViewResetClicked() {
        mPresenter.editPhone(mEdtForgetPhone.getText().toString().trim(), mEdtForgetCode.getText().toString().trim(),0);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (!TextUtils.isEmpty(mEdtForgetPhone.getText().toString()) &&
                !TextUtils.isEmpty(mEdtForgetCode.getText().toString())) {
            mBtnReset.setEnabled(true);
        } else {
            mBtnReset.setEnabled(false);
        }
    }

    public void onGetCode() {
        final int count = 60;//倒计时60秒
        Observable.interval(0, 1, TimeUnit.SECONDS)
                .take(count + 1)
                .map(new Function<Long, Long>() {
                    @Override
                    public Long apply(Long aLong) throws Exception {
                        return count - aLong;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())//ui线程中进行控件更新
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        mBtnForgetPostCode.setEnabled(false);
                        mBtnForgetPostCode.setTextColor(AM.res().getColor(R.color.text_secondary));
                    }
                }).subscribe(new Observer<Long>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Long num) {
                mBtnForgetPostCode.setText(num + "s");
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {
                //回复原来初始状态
                mBtnForgetPostCode.setEnabled(true);
                mBtnForgetPostCode.setText("获取验证码");
                mBtnForgetPostCode.setTextColor(AM.res().getColor(R.color.primary_highlight));
            }
        });

    }

    @Override
    public void showGetCodeSuccess() {
        onGetCode();
    }

    @Override
    public void showGetCodeError(String errorMsg) {
        showMsg(errorMsg);
    }

    @Override
    public void showResetPwdSuccess() {
        Intent intent=new Intent(this,NewPhoneActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void showResetPwdError(String errorMsg) {
        showMsg(errorMsg);
    }




}